package com.parking.carsystem.parkingchargesystem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alipay.sdk.app.EnvUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.common.Constant;
import com.parking.carsystem.parkingchargesystem.module.AbsenceData;
import com.parking.carsystem.parkingchargesystem.module.RechargeModel;
import com.parking.carsystem.parkingchargesystem.view.BottomDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import ttyy.com.recyclerexts.base.EXTRecyclerAdapter;
import ttyy.com.recyclerexts.base.EXTViewHolder;

public class RechargeMoneyActivity extends BaseActivity implements View.OnClickListener, BottomDialog.ChooseTextLisenter {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.text)
    TextView text;
    @BindView(R.id.check_pay_record)
    TextView checkPayRecord;
    @BindView(R.id.money)
    EditText money;
    @BindView(R.id.pay_immediate_purchase)
    TextView payImmediatePurchase;
    @BindView(R.id.setting)
    TextView setting;
    @BindView(R.id.title_bg)
    RelativeLayout titleBg;
    @BindView(R.id.symbol)
    TextView symbol;
    @BindView(R.id.layout_top)
    RelativeLayout layoutTop;
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    private ArrayList<AbsenceData> absenceDataPay;
    private BottomDialog bottomDialogPay;
    private String amountMoney;
    private RechargeModel rechargeModel;
    private EXTRecyclerAdapter<AbsenceData> recyclerAdapter;
    private ArrayList<AbsenceData> list;
    private AbsenceData tempAbsenceData = null;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_recharge_money;
    }

    @Override
    public void initView() {
        //支付宝的开启沙箱环境支付
//        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        back.setVisibility(View.VISIBLE);
        text.setText("充值");
        absenceDataPay = new ArrayList<>();
        absenceDataPay.add(new AbsenceData("支付宝", true));
        absenceDataPay.add(new AbsenceData("微信", false));
        bottomDialogPay = new BottomDialog(absenceDataPay.size(), this,1);
        bottomDialogPay.setChooseTextLisenter(this);
        bottomDialogPay.setTitle("支付方式");
        bottomDialogPay.setBottomTitle("确认支付");
        bottomDialogPay.setBottomData(absenceDataPay);
        payImmediatePurchase.setOnClickListener(this);
        checkPayRecord.setOnClickListener(this);
    }

    @Override
    public void initData() {
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(new AbsenceData("10", false));
        list.add(new AbsenceData("30", false));
        list.add(new AbsenceData("50", false));
        list.add(new AbsenceData("100", false));
        list.add(new AbsenceData("200", false));
        list.add(new AbsenceData("300", false));
        recycleView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerAdapter = new EXTRecyclerAdapter<AbsenceData>(R.layout.item_money) {
            @Override
            public void onBindViewHolder(EXTViewHolder extViewHolder, int i, AbsenceData data) {
                TextView money = extViewHolder.findViewById(R.id.money);
                money.setText(data.text + "元");
                if (data.choose) {
                    money.setTextColor(getResources().getColor(R.color.color_ff0462e2));
                    money.setBackgroundResource(R.drawable.recharge_count_bg_blue);
                } else {
                    money.setTextColor(getResources().getColor(R.color.color_d9000000));
                    money.setBackgroundResource(R.drawable.recharge_count_bg);
                }

            }
        };
        recyclerAdapter.setDatas(list);
        recycleView.setAdapter(recyclerAdapter);
        recyclerAdapter.setOnItemClickListener(new EXTRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClicked(View view, int i) {
                AbsenceData absenceData = list.get(i);
                if (!absenceData.choose) {
                    absenceData.choose = true;
                    if (tempAbsenceData != null) {
                        tempAbsenceData.choose = false;
                    }
                }
                tempAbsenceData = absenceData;
                recyclerAdapter.notifyDataSetChanged();
                money.setText(tempAbsenceData.text);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pay_immediate_purchase:
                amountMoney = money.getText().toString();
                if (!TextUtils.isEmpty(amountMoney)) {
                    selectPrePayRechargeOrder();
                }
                break;
            case R.id.check_pay_record:
                Intent intent = new Intent(this, RechargeRecordActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 生成充值预付订单
     */
    private void selectPrePayRechargeOrder() {
        showLoading();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("fee", amountMoney);
        JSONObject jsonObject = new JSONObject(hashMap);
        OkGo.<String>post(Constant.url + Constant.SELECTPREPAYRECHARGEORDER)
                .upJson(jsonObject.toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        stopLoading();
                        Gson gson = new Gson();
                        rechargeModel = gson.fromJson(response.body(), RechargeModel.class);
                        if (rechargeModel.code == 200) {
                            bottomDialogPay.show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        stopLoading();
                    }
                });

    }

    @Override
    public void chooseText(AbsenceData text) {
        //点击返回的内容
        if (text.text.equals("支付宝") || text.text.equals("微信")) {
            if (text.text.equals("微信")) {
                payWeiXin(rechargeModel.data.outTradeNo);
            } else if (text.text.equals("支付宝")) {
                payAli(rechargeModel.data.outTradeNo);
            }
        }
    }

    public void handleMessageMeathed(Message msg) {
        boolean payResponse = payResponse(msg);
        if (payResponse) {
            setResult(Constant.MONTHLYSUBSCRIPTIONPARKING);
            finish();
        }
    }
}
